﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;


namespace Ivy.SiteAdmin.DataAccess
{
    public class ContentFeedbackDao :IDaoInterface<ContentFeedback, ContentFeedbackCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #region IDaoInterface<ContentFeedback, ContentFeedbackCriteria> Members
        public bool Insert(ContentFeedback ContentFeedbackEntity)
        {
            bool IsOperationSuccessful = false;
            
                if (ContentFeedbackEntity.EntityState == EntityState.New)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Dasideen_ContentFeedback_Insert";
                    CmdObj.CommandType = CommandType.StoredProcedure;

                   
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackContentKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackEntity.ContentFeedbackContentKey);
                    if (ContentFeedbackEntity.ContentFeedbackFeedbackKey > 0)
                    {
                        DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackFeedbackKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackEntity.ContentFeedbackFeedbackKey);
                    }
                    else
                    {
                        DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackFeedbackKey", ParameterDirection.Input, DbType.Int32, DBNull.Value);
                    }
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackText", ParameterDirection.Input, DbType.String, ContentFeedbackEntity.ContentFeedbackText);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackDate", ParameterDirection.Input, DbType.DateTime, ContentFeedbackEntity.ContentFeedbackDate);
                    if (ContentFeedbackEntity.ContentFeedbackAuthorUserKey > 0)
                    {
                        DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackAuthorUserKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackEntity.ContentFeedbackAuthorUserKey);
                    }
                    else
                    {
                        DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackAuthorUserKey", ParameterDirection.Input, DbType.Int32, DBNull.Value);
                    }
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackAuthorName", ParameterDirection.Input, DbType.String, ContentFeedbackEntity.ContentFeedbackAuthorName);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackAuthorEmail", ParameterDirection.Input, DbType.String, ContentFeedbackEntity.ContentFeedbackAuthorEmail);


                    DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ContentFeedbackEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);
                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecord = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecord == 1)
                    {
                        ContentFeedbackEntity.ContentFeedbackKey = Convert.ToInt32(CmdObj.Parameters["@ContentFeedbackKey"].Value);
                        ContentFeedbackEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                        ContentFeedbackEntity.Sys_DateOfModification = ContentFeedbackEntity.Sys_DateOfCreation;
                        ContentFeedbackEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }
                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {

                    throw new Exception("For Inserting new ContentFeedback entity data to database,[EntityState] attribute should be [New]");
                }
            
            return IsOperationSuccessful;
        }

        public bool Update(ContentFeedback ContentFeedbackEntity)
        {
            bool IsOperationSuccessful = false;
            
                if (ContentFeedbackEntity.EntityState == EntityState.Changed)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Dasideen_ContentFeedback_Update";
                    CmdObj.CommandType = CommandType.StoredProcedure;

                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackEntity.ContentFeedbackKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackContentKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackEntity.ContentFeedbackContentKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackFeedbackKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackEntity.ContentFeedbackFeedbackKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackText", ParameterDirection.Input, DbType.String, ContentFeedbackEntity.ContentFeedbackText);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackDate", ParameterDirection.Input, DbType.DateTime, ContentFeedbackEntity.ContentFeedbackDate);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackAuthorUserKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackEntity.ContentFeedbackAuthorUserKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackAuthorName", ParameterDirection.Input, DbType.String, ContentFeedbackEntity.ContentFeedbackAuthorName);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackAuthorEmail", ParameterDirection.Input, DbType.String, ContentFeedbackEntity.ContentFeedbackAuthorEmail);

                    DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, ContentFeedbackEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecords = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecords == 1)
                    {
                        ContentFeedbackEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                        ContentFeedbackEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }

                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {
                    throw new Exception("For Updating existing ContentFeedback entity data to database, [EntityState] attribute should be [Changed]");
                }
            
            return IsOperationSuccessful;
        }

        public bool Delete(ContentFeedback DataEntity)
        {
            throw new NotImplementedException();
        }

        public ContentFeedback GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<ContentFeedback> GetAll()
        {
            List<ContentFeedback> lContentFeedbacksList = new List<ContentFeedback>();
            
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentFeedback_GetAll";
                CmdObj.CommandType = CommandType.StoredProcedure;

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        var lContentFeedbacktemp = new ContentFeedback(); //IMPORTANT NOTE:- Create instance only when required.
                        this.TranslateContentFeedbackFieldsFromReaderToEntity(lContentFeedbacktemp, _DataReader);
                        lContentFeedbacksList.Add(lContentFeedbacktemp);
                    }
                }
                CmdObj.Dispose();
                DbConn.Close();

            return lContentFeedbacksList;
        }

        public List<ContentFeedback> GetByCriteria(ContentFeedbackCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }

        #endregion

        public ContentFeedback GetByContentFeedbackKey(int ContentFeedbackKey)
        {
            ContentFeedback _ContentFeedbackList = new ContentFeedback(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
            
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentFeedback_GetByContentFeedbackKey";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ContentFeedbackKey));


                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                DbDataReader _DataReader = CmdObj.ExecuteReader();

                if (_DataReader.HasRows)
                {
                    if (_DataReader.Read())
                    {
                        _ContentFeedbackList = new ContentFeedback(); //IMPORTANT NOTE:- Create instance only when required.
                        TranslateContentFeedbackFieldsFromReaderToEntity(_ContentFeedbackList, _DataReader);
                    }
                }
                else
                {
                    throw new Exception("No ContentFeedback found");
                }
                CmdObj.Dispose();
                DbConn.Close();
            return _ContentFeedbackList;
        }

        public List<ContentFeedbacks> GetAllContentFeedbackByContentCode(string ContentCode)
        {
            List<ContentFeedbacks> _ContentFeedbackList = new List<ContentFeedbacks>();
            
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentFeedback_GetByContentCode";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ContentCode", ParameterDirection.Input, DbType.String, ContentCode);
                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_IsDeleted", ParameterDirection.Input, DbType.Int32, 0);
                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_OrganizationID", ParameterDirection.Input, DbType.Int32, -1);


                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        ContentFeedbacks _TempContentFeedbacks = new ContentFeedbacks(); //IMPORTANT NOTE:- Create instance only when required.
                        this.TranslateContentFeedbacksFieldsFromReaderToEntity(_TempContentFeedbacks, _DataReader);
                        _ContentFeedbackList.Add(_TempContentFeedbacks);
                    }
                }
                CmdObj.Dispose();
                DbConn.Close();
            
            return _ContentFeedbackList;
        }

        public List<ContentFeedback> GetByContentFeedbackContentKey(int ContentFeedbackContentKey)
        {
            List<ContentFeedback> _ContentFeedbackList = new List<ContentFeedback>();
            
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentFeedback_GetByContentFeedbackContentKey";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackContentKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackContentKey);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        ContentFeedback _TempContentFeedback = new ContentFeedback(); //IMPORTANT NOTE:- Create instance only when required.
                        this.TranslateContentFeedbackFieldsFromReaderToEntity(_TempContentFeedback, _DataReader);
                        _ContentFeedbackList.Add(_TempContentFeedback);
                    }
                }
                CmdObj.Dispose();
                DbConn.Close();
            
            return _ContentFeedbackList;
        }

        public List<ContentFeedback> GetByContentFeedbackFeedbackKey(int ContentFeedbackFeedbackKey)
        {
            List<ContentFeedback> _ContentFeedbackList = new List<ContentFeedback>();
            
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentFeedback_GetByContentFeedbackFeedbackKey";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ContentFeedbackFeedbackKey", ParameterDirection.Input, DbType.Int32, ContentFeedbackFeedbackKey);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        ContentFeedback _TempContentFeedback = new ContentFeedback(); //IMPORTANT NOTE:- Create instance only when required.
                        this.TranslateContentFeedbackFieldsFromReaderToEntity(_TempContentFeedback, _DataReader);
                        _ContentFeedbackList.Add(_TempContentFeedback);
                    }
                }
                CmdObj.Dispose();
                DbConn.Close();
            
            return _ContentFeedbackList;
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateContentFeedbackFieldsFromReaderToEntity(ContentFeedback pContentFeedback, DbDataReader pDbDataReader)
        {
            
                pContentFeedback.ContentFeedbackKey = Convert.ToInt32(pDbDataReader["ContentFeedbackKey"]);
                pContentFeedback.ContentFeedbackContentKey = Convert.ToInt32(pDbDataReader["ContentFeedbackContentKey"]);
                pContentFeedback.ContentFeedbackFeedbackKey = Convert.ToInt32(pDbDataReader["ContentFeedbackFeedbackKey"]);
                pContentFeedback.ContentFeedbackText = Convert.ToString(pDbDataReader["ContentFeedbackText"]);
                pContentFeedback.ContentFeedbackDate = Convert.ToDateTime(pDbDataReader["ContentFeedbackDate"]);
                pContentFeedback.ContentFeedbackAuthorUserKey = Convert.ToInt32(pDbDataReader["ContentFeedbackAuthorUserKey"]);
                pContentFeedback.ContentFeedbackAuthorName = Convert.ToString(pDbDataReader["ContentFeedbackAuthorName"]);
                pContentFeedback.ContentFeedbackAuthorEmail = Convert.ToString(pDbDataReader["ContentFeedbackAuthorEmail"]);

                DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContentFeedback, pDbDataReader);
           
        }

        private void TranslateContentFeedbacksFieldsFromReaderToEntity(ContentFeedbacks pContentFeedbacks, DbDataReader pDbDataReader)
        {
            pContentFeedbacks.ContentFeedbackKey = Convert.ToInt32(pDbDataReader["ContentFeedbackKey"]);
            pContentFeedbacks.ContentFeedbackContentKey = Convert.ToInt32(pDbDataReader["ContentFeedbackContentKey"]);
            if (pDbDataReader["ContentFeedbackFeedbackKey"] != DBNull.Value)
            {
                pContentFeedbacks.ContentFeedbackFeedbackKey = Convert.ToInt32(pDbDataReader["ContentFeedbackFeedbackKey"]);
            }
            pContentFeedbacks.ContentFeedbackText = Convert.ToString(pDbDataReader["ContentFeedbackText"]);
            pContentFeedbacks.ContentFeedbackDate = Convert.ToDateTime(pDbDataReader["ContentFeedbackDate"]);
            if (pDbDataReader["ContentFeedbackAuthorUserKey"] != DBNull.Value)
            {
                pContentFeedbacks.ContentFeedbackAuthorUserKey = Convert.ToInt32(pDbDataReader["ContentFeedbackAuthorUserKey"]);
            }
            pContentFeedbacks.ContentFeedbackAuthorName = Convert.ToString(pDbDataReader["ContentFeedbackAuthorName"]);
            pContentFeedbacks.ContentFeedbackAuthorEmail = Convert.ToString(pDbDataReader["ContentFeedbackAuthorEmail"]);
            pContentFeedbacks.ContentCode = Convert.ToString(pDbDataReader["ContentCode"]);

            //DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContentFeedbacks, pDbDataReader);
       
        }
        #endregion Private Methods
        
    }
}
